package cn.itcast.bos.web.action.take_delivery;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.lucene.util.packed.PagedGrowableWriter;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import cn.itcast.bos.domain.take_delivery.WayBill;
import cn.itcast.bos.service.take_delivery.WayBillService;
import cn.itcast.bos.web.action.common.BaseAction;

@Controller
@Scope("prototype")
@Namespace("/")
@ParentPackage("json-default")
@SuppressWarnings("all")
public class WayBillAction extends BaseAction<WayBill> {

	private static final Logger LOGGER = Logger.getLogger(WayBillAction.class);

	@Autowired
	private WayBillService wayBillService;

	@Action(value = "waybill_save", results = { @Result(name = "success", type = "json") })
	public String save() {

		Map<String, Object> result = new HashMap<String, Object>();

		try {
			// 如果运单里面关联的订单没有存入数据库，那么此时我们需要将order设置为空，否则会报关联了一个瞬时态的对象
			if (model.getOrder() != null && (model.getOrder().getId() == null || model.getOrder().getId() == 0)) {

				model.setOrder(null);

			}

			wayBillService.save(model);

			// 对结果做处理，保存成功
			result.put("success", true);
			result.put("msg", "运单保存成功");
			LOGGER.info("运单保存成功，运单号是：" + model.getWayBillNum());
		} catch (Exception e) {

			e.printStackTrace();
			// 保存失败

			result.put("success", false);
			result.put("msg", "保存运单失败");
			LOGGER.error("保存运单失败，运单号：" + model.getWayBillNum(), e);

		}

		// 将结果存入值栈,页面接收参数要与设置的参数保持一致
		ActionContext.getContext().getValueStack().push(result);

		return SUCCESS;
	}

	/**
	 * 分页操作,做查询操作的时候，结果返回的type类型是json waybill_pageQuery,在waybill_manage.html里面的表格table里面也有发送分页请求
	 */
	@Action(value = "waybill_pageQuery", results = { @Result(name = "success", type = "json") })
	public String pageQuery() {
		// 查询的结果根据id升序排列
		Pageable pageable = new PageRequest(page - 1, rows, new Sort(new Sort.Order(Sort.Direction.ASC, "id")));

		// 调用业务层处理数据
		Page<WayBill> pageQuery = wayBillService.pageQuery(model,pageable);

		// 将页面需要的当前页的数据rows,和总记录数total返回给前端页面
		pushPageDataToValueStack(pageQuery);

		return SUCCESS;
	}

	/**
	 * 根据运单号查询快速录入时存入数据库的运单信息，将数据回显到运单录入页面中 waybill_findByWayBillNum
	 */
	@Action(value = "waybill_findByWayBillNum", results = { @Result(name = "success", type = "json") })
	public String findByWayBillNum() {

		WayBill wayBill = wayBillService.findByWayBillNum(model.getWayBillNum());

		// 将数据结果封装到map里面
		Map<String, Object> map = new HashMap<String, Object>();

		if (wayBill == null) {
			// 没有次运单
			map.put("success", false);
		} else {
			// 查询成功
			map.put("success", true);
			map.put("wayBillData", wayBill);

		}

		// 存入值栈
		ActionContext.getContext().getValueStack().push(map);

		return SUCCESS;
	}
}
